package com.briup.controller;

import com.briup.bean.Department;
import com.briup.service.DepartmentService;
import com.briup.util.Message;
import com.briup.util.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wyq
 * @create 2021-09-24 11:25
 */
@Api(description = "部门接口")
@RequestMapping("/department")
@Validated
@RestController
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @ApiOperation("添加部门")
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Department department) {
        int i = departmentService.saveOrUpdate(department);
        if (i != 0) {
            return MessageUtil.success("操作成功");
        }
        return MessageUtil.error("操作失败");
    }

    @ApiOperation("删除部门")
    @DeleteMapping("deleteDepartment")
    public Message deleteDepartment(int id) {
        int i = departmentService.deleteById(id);
        if (i != 0) {
            return MessageUtil.success("删除成功");
        }
        return MessageUtil.error("删除失败");
    }

    @ApiOperation("查询部门")
    @GetMapping("queryDepartment")
    public Message queryDepartment(int id) {
        Department department = departmentService.findById(id);
        if (department != null) {
            return MessageUtil.success("查询成功", department);
        }
        return MessageUtil.error("查询失败，没有这个部门");
    }

    @ApiOperation("查询部门根据名字")
    @GetMapping("queryByName")
    public Message queryByName(String name) {
        List<Department> byName = departmentService.findByName(name);
        return MessageUtil.success("查询成功", byName);
    }
}
